<template>
  <div class="header">
    <span>药品管理系统</span>
    <div>
      <span v-if="userName">欢迎管理员：{{ userName }}</span>
      <span v-else>欢迎使用系统</span>
      <button v-if="userName" @click="logout">退出登录</button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();

const userName = computed(() => {
  // 先尝试从路由参数中获取用户名
  let userNameFromParams = route.params.userName;
  if (userNameFromParams) {
    return userNameFromParams;
  }
  // 若路由参数中没有，再从 localStorage 中获取
  return localStorage.getItem('userName') || '';
});

const logout = () => {
  localStorage.removeItem('isLoggedIn');
  localStorage.removeItem('userName');
  router.push('/');
};
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #eaeaea;
  color: rgb(14, 13, 13);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

button {
  margin-left: 10px;
  padding: 5px 10px;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  background-color: #e74c3c;
  color: white;
}

button:hover {
  background-color: #c0392b;
}
</style>